import React from 'react'
import { BrowserRouter as Router, Switch, Route, Redirect } from 'react-router-dom'

const beforeEnter = (item, History) => { //路由拦截
    if (item.islogin) {
        const backToken = window.localStorage.getItem("backToken");
        const authList=window.localStorage.getItem("auth")&& window.localStorage.getItem("auth").split(',');
        // console.log(backToken,'backtoken')
        if (!backToken) {
            return <Redirect to="/login"></Redirect>

        }else{
            if(!authList.some(v=>item.roles===v)){
                return <Redirect to="/four"></Redirect>
            }
        }
    }
    return <item.component routes={item.children ? item.children : []} {...History}>
        <RouteView routes={item.children ? item.children : []} />
    </item.component>
}
const RouteView = (props) => {
    const { routes } = props;
    return <Switch>
        {
            routes.map((item, index) => item.path ?
                <Route
                    key={index}
                    path={item.path}
                    render={(History) => beforeEnter(item, History)}
                />
                : <Redirect key={index} {...item} exact />)
        }

    </Switch>
}
export default RouteView